﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Core;
using Sisocana.Seguridad;

namespace Sisocana.Controllers
{
    public class ONGController : Controller
    {
        private IONGService _ONGService;
        public ONGController()
            : this(new ONGService())
        {
        }

        public ONGController(IONGService ONGService)
        {
            _ONGService = ONGService;
        }

        //
        // GET: /ONG/
        [AdminGralAuthorize]
        public ActionResult Index()
        {
            IList<ONGModel> ONGs = _ONGService.GetAll();
            return View(ONGs);
        }

        //
        // GET: /ONG/Details/5
        public ActionResult state(int id)
        {
            ONGModel ONG = _ONGService.GetSingle(a => a.id == id);
            return View(ONG);
        }

        [HttpPost]
        public ActionResult state(ONGModel ong)
        {
            try
            {
                int res = _ONGService.disableONG(ong.id);
                if (res == 0)
                    return RedirectToAction("Index", "ONG");
                if (res == -1)
                {
                    ModelState.AddModelError("", "There is no ONG with the current id");
                    return View(ong);
                }
                else
                {
                    ModelState.AddModelError("", "The ONG is already disabled for donations");
                    return View(ong);
                }

            }
            catch (Exception e)
            {
                return View(ong);
            }
        }

        //
        // GET: /ONG/Create
        [AdminGralAuthorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ONG/Create

        [HttpPost]
        public ActionResult Create(ONGModel ong)
        {
            try
            {
                if (_ONGService.createONG(ong) == 0)
                    return RedirectToAction("Index");
                else
                {
                    ModelState.AddModelError("", "There are problems with te create action");
                    return View(ong);
                }

            }
            catch
            {
                return View(ong);
            }
        }
        
        //
        // GET: /ONG/Edit/5
        [AdminGralAuthorize]
        public ActionResult Edit(int id)
        {
            ONGModel ONG = _ONGService.GetSingle(a => a.id == id);
            return View(ONG);
        }

        //
        // POST: /ONG/Edit/5

        [HttpPost]
        public ActionResult Edit(ONGModel ong)
        {
            try
            {
                int res = _ONGService.updateONG(ong);
                if (res == 0)
                    return RedirectToAction("Index");
                else
                {
                    if (res == -1)
                    {
                        ModelState.AddModelError("", "The current ONG does exist.");
                        return View(ong);
                    }
                    else
                    {
                        ModelState.AddModelError("", "The are problems with the update");
                        return View(ong);
                    }
                }
            }
            catch
            {
                ModelState.AddModelError("", "The are problems with the update");
                return View(ong);
            }
        }


    }
}
